import "reflect-metadata";
import { ZExcel } from '../init';

export function Factory(target: any,container: HTMLElement, config: any) {
    return getTypes(target, <string>container.getAttribute('id'))
}
export function getTypes(target: any, id: string) {
    let types = (<any>Reflect).getMetadata("design:paramtypes", target);
    try {
        for(let i = 0; i < types.length; i++) {
            if(Object.prototype.toString.call(types[i]) == '[object Function]') {
                types[i] = getTypes(types[i], id);
            }
        }        
    } catch (error) {
        
    }
    if(ZExcel.instance.get(id) == undefined) {
        ZExcel.instance.set(id, {classes: new Map()})
    }
    if(ZExcel.instance.get(id).classes.get(target.prototype.constructor.name) == undefined) {
        target.prototype.containerId = id;
        let instance = new target(...types);
        if(instance.zInit != undefined) instance.zInit();
        ZExcel.instance.get(id).classes.set(target.prototype.constructor.name, instance)
    }
    return ZExcel.instance.get(id).classes.get(target.prototype.constructor.name);
}